package com.ccl.iot.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.ccl.iot.R;
import com.ccl.iot.publics.UIUtils;

/**
 * 创建者     王浩
 * 创建时间   2017/4/5 16:10
 * 描述	      ${TODO}
 */
public class PrecisionLocationView extends View {

    private final int DRAW_SPEED = 5;

    private Paint mAnimPaintG, mAnimPaintDG, mBeaconPaint, mUserPaint, mMaskPaint;
    private RadialGradient mRadialGradient, mRadialGradient2;
    private int mHeight;
    private int mWidth;
    private int mBeaconRadius;

    private int mFullRadius;
    private Bitmap mUser;

    private int mGRaios = 1;
    private int mDGRaios = 0;
    private int mD;
    private int mE;
    private int mXRadius;
    private int mYRadius;
    //    private Bitmap mUser;

    private int mAngle = 2;
    private int mOffset = 0;
    private int mBaseOffset = 0;

    public static float LOCATION_ACCURACY = 0.8f;

    public PrecisionLocationView(Context context) {
        this(context, null);
    }

    public PrecisionLocationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PrecisionLocationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mAnimPaintG = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAnimPaintG.setStyle(Paint.Style.FILL);
        mD = Color.parseColor("#dddddd");
        //        mAnimPaintG.setColor(mD);

        mAnimPaintDG = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAnimPaintDG.setStyle(Paint.Style.FILL);
        mE = Color.parseColor("#eeeeee");
        //        mAnimPaintDG.setColor(mE);

        mBeaconPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBeaconPaint.setStyle(Paint.Style.FILL);
        mBeaconPaint.setColor(Color.GREEN);

        mUserPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mUserPaint.setStyle(Paint.Style.FILL);
        mUserPaint.setColor(Color.RED);
        //        PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        //        mUserPaint.setXfermode(porterDuffXfermode);

        mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mMaskPaint.setStyle(Paint.Style.FILL);
        mMaskPaint.setColor(Color.RED);

        mBeaconRadius = UIUtils.dp2px(10);

        //        mUser = UIUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.smart_red_user), mBeaconRadius << 1, mBeaconRadius << 1);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = getHeight() - mBeaconRadius * 2;
        mWidth = getWidth() - mBeaconRadius * 2;

        //        mHeight = getHeight() - mUser.getHeight();
        //        mWidth = getWidth() - mUser.getWidth();

        mXRadius = mWidth / 2;
        mYRadius = mHeight / 2;

        mFullRadius = mXRadius > mYRadius ? mYRadius : mXRadius;
        mBaseOffset = mFullRadius / 4 * 3;

        mRadialGradient = new RadialGradient(mXRadius, mYRadius, mFullRadius,
                new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

        mRadialGradient2 = new RadialGradient(mXRadius, mYRadius, mFullRadius,
                new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

        mAnimPaintG.setShader(mRadialGradient);
        mAnimPaintDG.setShader(mRadialGradient2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < 2; i++) {
            if (i == 0) {
                mOffset = -mBaseOffset;
            } else if (i == 1) {
                mOffset = mBaseOffset;
            }
            if (mGRaios > mDGRaios) {

                canvas.drawCircle(mXRadius + mBeaconRadius + mOffset, mYRadius + mBeaconRadius, mFullRadius, mAnimPaintDG);
                mGRaios += DRAW_SPEED;

                if (mGRaios > mFullRadius) {
                    mGRaios = mFullRadius;
                }
                mRadialGradient = new RadialGradient(mXRadius + mOffset, mYRadius, mGRaios,
                        new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

                mAnimPaintG.setShader(mRadialGradient);
                canvas.drawCircle(mXRadius + mBeaconRadius + mOffset, mYRadius + mBeaconRadius, mGRaios, mAnimPaintG);

                if (mGRaios >= mFullRadius / 2) {
                    mDGRaios += DRAW_SPEED;

                    if (mDGRaios > mFullRadius) {
                        mDGRaios = mFullRadius;
                    }

                    mRadialGradient2 = new RadialGradient(mXRadius + mOffset, mYRadius, mDGRaios,
                            new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

                    mAnimPaintDG.setShader(mRadialGradient2);
                    canvas.drawCircle(mXRadius + mBeaconRadius + mOffset, mYRadius + mBeaconRadius, mDGRaios, mAnimPaintDG);
                }
            } else {
                canvas.drawCircle(mXRadius + mBeaconRadius + mOffset, mYRadius + mBeaconRadius, mFullRadius, mAnimPaintG);
                mDGRaios += DRAW_SPEED;

                if (mDGRaios > mFullRadius) {
                    mDGRaios = mFullRadius;
                }

                mRadialGradient2 = new RadialGradient(mXRadius + mOffset, mYRadius, mDGRaios,
                        new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

                mAnimPaintDG.setShader(mRadialGradient2);
                canvas.drawCircle(mXRadius + mBeaconRadius + mOffset, mYRadius + mBeaconRadius, mDGRaios, mAnimPaintDG);

                if (mDGRaios >= mFullRadius / 2) {
                    mGRaios += DRAW_SPEED;

                    if (mGRaios > mFullRadius) {
                        mGRaios = mFullRadius;
                    }

                    mRadialGradient = new RadialGradient(mXRadius + mOffset, mYRadius, mGRaios,
                            new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

                    mAnimPaintG.setShader(mRadialGradient);
                    canvas.drawCircle(mXRadius + mBeaconRadius + mOffset, mYRadius + mBeaconRadius, mGRaios, mAnimPaintG);
                }
            }
            canvas.drawCircle(mXRadius + mBeaconRadius + mOffset, mYRadius + mBeaconRadius, mBeaconRadius, mBeaconPaint);
            //        canvas.drawCircle(x + mBeaconRadius, y + mBeaconRadius, mBeaconRadius, mUserPaint);
            //        canvas.drawBitmap(mUser, x, y, mUserPaint);

            mAngle = (mAngle % 360) + 2;
            if (mGRaios >= mFullRadius) {
                mGRaios = 0;
            }

            if (mDGRaios >= mFullRadius) {
                mDGRaios = 0;
            }
        }

        if (mMask == null) {
            mMask = createCircleImage();
        }

        canvas.drawBitmap(mMask, mXRadius + mBeaconRadius - mBaseOffset - mFullRadius, getHeight() / 2 - mMask.getHeight() / 2, mUserPaint);

        if (!isOnCenter) {
            canvas.drawBitmap(mUser, getWidth() - mUser.getWidth()*2, getHeight() - mUser.getHeight()*2, mUserPaint);
        } else {
            canvas.drawBitmap(mUser, getWidth() / 2 - mUser.getWidth()/2, getHeight() / 2 - mUser.getHeight()/2, mUserPaint);
        }

        postDelayed(drawTask, 100);
    }

    private Bitmap mMask = null;

    private Bitmap createCircleImage() {
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);

        Bitmap des = Bitmap.createBitmap(mFullRadius * 2 + mBaseOffset * 2, mFullRadius * 2, Bitmap.Config.ARGB_8888);
        Bitmap src = Bitmap.createBitmap(mFullRadius * 2 + mBaseOffset * 2, mFullRadius * 2, Bitmap.Config.ARGB_8888);
        // 产生一个同样大小的画布
        Canvas desCanvas = new Canvas(des);
        Canvas srcCanvas = new Canvas(src);

        // 首先绘制圆形
        desCanvas.drawCircle(mFullRadius, mFullRadius, mFullRadius, paint);
        srcCanvas.drawCircle(mFullRadius + mBaseOffset * 2, mFullRadius, mFullRadius, paint);

        Bitmap resultbtp = Bitmap.createBitmap(mFullRadius * 2 + mBaseOffset * 2, des.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas resultCan = new Canvas(resultbtp);

        resultCan.drawBitmap(des, 0, 0, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        resultCan.drawBitmap(src, 0, 0, paint);
        paint.setXfermode(null);

        return resultbtp;
    }

    private Runnable drawTask = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };

    private boolean isOnCenter;

    public void onCenter(boolean isOnCenter) {
        this.isOnCenter = isOnCenter;
        if (isOnCenter) {
            mUser = UIUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.smart_green_user), mBeaconRadius << 1, mBeaconRadius << 1);
        }else{
            mUser = UIUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.smart_red_user), mBeaconRadius << 1, mBeaconRadius << 1);
        }
    }

}
